#include <conio.h>
#include <graphics.h>
#include<math.h>
#pragma comment(lib,"Winmm.lib")

#define high 700  // 游戏画面尺寸
#define width 500

// 全局变量
IMAGE img_bk,img_planenormal1,img_planenormal2,img_bullet1,img_bullet2,img_enemyplane1,img_enemyplane2, img_planeExplode_1,img_planeExplode_2;
float enemyposition_x, enemyposition_y;
int position_x, position_y;
int bullet_x, bullet_y;
int k = 0;

void startup()  // 数据初始化
{
	mciSendString("open E:\\game_music.mp3 alias bkmusic", NULL, 0, NULL);
	mciSendString("play bkmusic repeat", NULL, 0, NULL);
	position_x = width / 2;
	position_y = high / 2;
	enemyposition_x = rand() % width / 2.0;
	enemyposition_y = -60;
	bullet_x = position_x;
	bullet_y = -100;
	initgraph(width, high);
	loadimage(&img_bk, "E:\\background.jpg");
	loadimage(&img_planenormal1, "E:\\planeNormal_1.jpg");
	loadimage(&img_planenormal2,"E:\\planeNormal_2.jpg");
	loadimage(&img_bullet1, "E:\\bullet1.jpg");
	loadimage(&img_bullet2, "E:\\bullet2.jpg");
	loadimage(&img_enemyplane1, "E:\\enemyPlane1.jpg");
	loadimage(&img_enemyplane2, "E:\\enemyPlane2.jpg");
	loadimage(&img_planeExplode_1, "E:\\planeExplode_1.jpg");
	loadimage(&img_planeExplode_2, "E:\\planeExplode_2.jpg");
	BeginBatchDraw();
}

void clean()  // 显示画面
{
	// 绘制黑线、黑色填充的圆


}

void show()  // 显示画面
{
	putimage(0, 0, &img_bk);
	putimage(position_x, position_y, &img_planenormal1, NOTSRCERASE);//显示正常飞机
	putimage(position_x, position_y, &img_planenormal2, SRCINVERT);
	putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);//显示子弹
	putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);
	putimage(enemyposition_x, enemyposition_y, &img_enemyplane1, NOTSRCERASE);//显示敌机
	putimage(enemyposition_x, enemyposition_y, &img_enemyplane2, SRCINVERT);
	if (fabs(enemyposition_x - position_x)<40 && fabs(enemyposition_y- position_y)<135&&position_y>enemyposition_y) {
		putimage(position_x, position_y, &img_planeExplode_1, NOTSRCERASE);//显示爆炸飞机
		putimage(position_x, position_y, &img_planeExplode_2, SRCINVERT);
		k = 1;
	}
	if (k==1) {
		settextcolor(YELLOW);
		settextstyle(40, 0, _T("宋体"));
		outtextxy(width/2-50, high/2, "游戏结束");
	}
	FlushBatchDraw();
	// 延时
	Sleep(3);
	if (k == 1) {
		system("pause");
	}
}

void updateWithoutInput()  // 与用户输入无关的更新
{
	if (bullet_y > -300) {
		bullet_y -= 5;
	}
	if (enemyposition_y < 700) {
		enemyposition_y += 0.5;
	}
	else {
		enemyposition_x = rand() % width * 0.5;
		enemyposition_y = -60;
	}
	if ((bullet_x<enemyposition_x+80&& bullet_x > enemyposition_x-6)&&(fabs(bullet_y-enemyposition_y)<5)) {

		mciSendString("close gotEnemy", NULL, 0, NULL);
		mciSendString("open E:\\gotEnemy.mp3 alias gotEnemy", NULL, 0, NULL);
		mciSendString("play gotEnemy", NULL, 0, NULL);
		enemyposition_x = rand() % width * 0.5;
		enemyposition_y = -60;
	}
}

void updateWithInput()  // 与用户输入有关的更新
{
	MOUSEMSG m;
	while (MouseHit()) {
		m = GetMouseMsg();
		if (m.uMsg == WM_MOUSEMOVE) {
			position_x = m.x - 45;
			position_y = m.y - 48;
		}
		else if (m.uMsg == WM_LBUTTONDOWN) {
			bullet_x = position_x + 43;
			bullet_y = position_y - 6;
			mciSendString("close f_gun", NULL, 0, NULL);
			mciSendString("open E:\\f_gun.mp3 alias f_gun", NULL, 0, NULL);
			mciSendString("play f_gun", NULL, 0, NULL);
		}
	}
	/*char input;
	if (kbhit()) {
		input = getch();
		if (input == ' ') {

		}
	}*/
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		clean();  // 把之前绘制的内容清除
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
		show();  // 显示新画面
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}


